package com.woniu.p2p.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.p2p.entity.Coupon;
import com.woniu.p2p.entity.UserCoupon;
import com.woniu.p2p.service.P2PCouponService;
import com.woniu.p2p.vo.CouponAndUserVo;
import com.woniu.p2p.vo.FindMessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 86152
 * @create 2020/8/11
 * @since 1.0.0
 */
@RestController
@RequestMapping("/coupon")
public class P2PCouponController {

    @Autowired
    P2PCouponService p2PCouponService;

    /*查询优惠券信息*/
    @RequestMapping("/findallcoupon")
    public PageInfo<UserCoupon> findallcoupon(HttpServletRequest request){
        String token = request.getHeader("token");
        int userid = Integer.parseInt(token);
        PageInfo<UserCoupon> userCouponPageInfo=p2PCouponService.findallcoupon(1,4,userid);
        return userCouponPageInfo;
    }

    /*根据当前页码获取分页数据*/
    @RequestMapping("/findsinglecouponpage")
    public PageInfo<UserCoupon> findsinglecouponpage(@RequestBody FindMessageVo findMessageVo, HttpServletRequest request){
        String token = request.getHeader("token");
        int userid = Integer.parseInt(token);
        PageInfo<UserCoupon> userCouponPageInfo=p2PCouponService.findallcoupon(findMessageVo.getPageNum(),findMessageVo.getPageSize(),userid);
        return userCouponPageInfo;
    }

    /*查询用户和优惠券信息*/
    @RequestMapping("/findcouponbyid")
    public CouponAndUserVo findcouponbyid(@RequestBody Coupon coupon, HttpServletRequest request){
        String token = request.getHeader("token");
        int userid = Integer.parseInt(token);
        Integer couponId = coupon.getCouponId();
        CouponAndUserVo couponAndUserVo=p2PCouponService.findcouponanduserbyid(userid,couponId);
        return couponAndUserVo;
    }
}
